package com.jie.other.compose.study04

import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

enum class PeopleUserInputAnimState {VAILD , INVAILD}

class PeopleUserInputState {

    var people by mutableStateOf(1)
    private set

    val animState : MutableTransitionState<PeopleUserInputAnimState>
        = MutableTransitionState(PeopleUserInputAnimState.VAILD)

    fun addPerson() {
        people = (people % (MAX_PEOPLE + 1)) + 1
        updateAnimState()
    }

    private fun updateAnimState() {
        val newState = if (people > MAX_PEOPLE) {
            PeopleUserInputAnimState.INVAILD
        } else {
            PeopleUserInputAnimState.VAILD
        }
        animState.targetState = newState
    }
}

@Composable
fun PeopleUserInput(
    titleSuffix : String? = "",
    onPeopleChange : (Int) -> Unit,
    peopleState : PeopleUserInputState = remember {
        PeopleUserInputState()
    },
    modifier: Modifier = Modifier,
) {
    Column(modifier = modifier.padding(horizontal = 16.dp)) {
        val animState = remember {
            peopleState.animState
        }
        Button(onClick = {
            peopleState.addPerson()
            onPeopleChange(peopleState.people)
        }) {
            Text(text = "${peopleState.people}$titleSuffix")
        }
        if (animState.targetState == PeopleUserInputAnimState.INVAILD) {
            Text(
                text = "错误！超过最大人数支持$MAX_PEOPLE",
                style = MaterialTheme.typography.body1.copy(color = MaterialTheme.colors.secondary)
            )
        }
    }

}